/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package run;

/**
 *
 * @author Toan
 */
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client extends  Thread{
    private Socket socket = null;
    private MyInfo info = null;
    private ClientStartGame csg;

    public final int ANSWER_TYPE = 1;
    public final int QUESTION_TYPE = 2;
    public final int MARK_TYPE = 3;

    public Client(String ServerIP,int ServerPort,MyInfo _info, ClientStartGame csg){
        this.csg = csg;
                try {
            /*Connect*/
            socket = new Socket(InetAddress.getByName(ServerIP), ServerPort);
            info = _info;
            csg = new ClientStartGame(_info, socket);
            DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
            outToServer.writeBytes("1#"+info.Username+'\n');
            outToServer.flush();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
       public void run() {
        while (true){
            BufferedReader inFromServer = null;
            try{
                inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String message = inFromServer.readLine();

                System.out.println("Your mark :"+message);
                csg.setMark(message);
                /* Xy ly:
                 * ham trong ClientStartGame : xyly(message);
                 * cgs.xyly(message);
                 */
               // xuly(message);
            }catch( Exception e){
                System.out.println("loi nay");
            }
        }
    }

 }

